﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using BaoJianSoft.RapidWeb.Properties;

namespace BaoJianSoft.RapidWeb.Controls
{
    /// <summary>
    /// Float dialog supports to wrap any content.
    /// </summary>
    public class ModalPanel : WebControl
    {
        private ModalPopupExtender modelPopupExtender;
        private Panel panelContentTemplate;

        /// <summary>
        /// Header text
        /// </summary>
        public string HeaderText { get; set; }

        /// <summary>
        /// ID of target control to fire event to display dialog.
        /// </summary>
        public string TargetControlID { get; set; }

        /// <summary>
        /// Sets/gets true to create a drop shadow by AjaxControlToolkit
        /// </summary>
        public bool DropShadow { get; set; }

        /// <summary>
        /// Sets/gets script on clicking ok button.
        /// </summary>
        public string OnOkScript { get; set; }

        /// <summary>
        /// Sets/gets script on clicking cancel button.
        /// </summary>
        public string OnCancelScript { get; set; }

        [Browsable(false), DefaultValue(null), Description("Inner template of ModalPanel."), PersistenceMode(PersistenceMode.InnerProperty),]
        public virtual ITemplate ContentTemplate { set; get; }

        public ModalPanel()
        {
            this.Width = new Unit("560px");
            this.DropShadow = true;
        }

        /// <summary>
        /// Event fires when model panel saved.
        /// </summary>
        public event EventHandler Saved;

        /// <summary>
        /// Event fires when model panel cancelled.
        /// </summary>
        public event EventHandler Cancelled;

        /// <summary>
        /// Raises the System.Web.UI.Control.Init event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.ValidateProperties();

            // Panel - Popup
            Panel panelPopup = new Panel()
            {
                ID = "PanelPopup_ModalPanel",
                CssClass = "modalpopup",
                Width = base.Width,
                Height = base.Height,
            };

            panelPopup.Style.Add("display", "none");
            this.Controls.Add(panelPopup);

            // Panel - Popup header
            panelPopup.Controls.Add(this.CreatePanelHeaderControl());

            // Panel - ITemplate
            if (this.ContentTemplate != null)
            {
                this.panelContentTemplate = new Panel();
                this.panelContentTemplate.Style.Add("padding-top", "4px");
                this.panelContentTemplate.Width = new Unit("100%");
                panelPopup.Controls.Add(this.panelContentTemplate);
                this.ContentTemplate.InstantiateIn(this.panelContentTemplate);
            }

            // ModelPopup Extender
            this.modelPopupExtender = new ModalPopupExtender()
            {
                ID = "ModalPopupExtenderModalPanel",
                TargetControlID = this.TargetControlID,
                PopupControlID = panelPopup.ID,
                BackgroundCssClass = "modalbackground",
                DropShadow = this.DropShadow,
                OkControlID = "LinkButtonOK_ModalPanel",
                CancelControlID = "LinkButtonClose_ModalPanel",
                OnOkScript = this.OnOkScript,
                OnCancelScript = this.OnCancelScript
            };

            this.Controls.Add(this.modelPopupExtender);
        }

        /// <summary>
        /// Show model popup dialog.
        /// </summary>
        public void Show()
        {
            this.modelPopupExtender.Show();
        }

        /// <summary>
        /// Hide model popup dialog.
        /// </summary>
        public void Hide()
        {
            this.modelPopupExtender.Hide();
        }

        /// <summary>
        /// Find control match specified id in content template.
        /// </summary>
        /// <param name="controlId"></param>
        /// <returns></returns>
        public new Control FindControl(string controlId)
        {
            return FindControl(this.panelContentTemplate, controlId);
        }

        private void linkButtonOK_Click(object sender, EventArgs e)
        {
            if (this.Saved != null) this.Saved(this, new EventArgs());
        }

        private void linkButtonClose_Click(object sender, EventArgs e)
        {
            if (this.Cancelled != null) this.Cancelled(this, new EventArgs());
        }

        private Control CreatePanelHeaderControl()
        {
            // Panel - Popup Header
            Panel panelPopupHeader = new Panel()
            {
                ID = "PanelPopupHeader_ModalPanel",
                CssClass = "modalpopupheader",
            };

            // Panel - Popup Header - Inner Table
            HtmlTable htmlTable = new HtmlTable()
            {
                Border = 0,
                CellSpacing = 0,
                CellPadding = 0,
                Width = "99%",
            };

            panelPopupHeader.Controls.Add(htmlTable);

            HtmlTableRow htmlTableRow = new HtmlTableRow();
            htmlTable.Rows.Add(htmlTableRow);

            HtmlTableCell htmlTableCell = new HtmlTableCell("td") { Width = "95%" };
            htmlTableCell.Attributes["class"] = "label";
            htmlTableCell.NoWrap = true;
            htmlTableRow.Cells.Add(htmlTableCell);
            Image imageLabel = new Image() { ImageUrl = this.GetIconUrl() };
            htmlTableCell.Controls.Add(imageLabel);
            HtmlGenericControl label = new HtmlGenericControl("span") { InnerHtml = "&nbsp;" + this.HeaderText };
            htmlTableCell.Controls.Add(label);

            htmlTableCell = new HtmlTableCell("td") { Width = "5%" };
            htmlTableCell.Attributes["class"] = "button";
            htmlTableCell.Style.Add("text-align", "right");
            htmlTableCell.NoWrap = true;
            htmlTableRow.Cells.Add(htmlTableCell);

            LinkButton linkButtonOK = new LinkButton() { ID = "LinkButtonOK_ModalPanel" };
            linkButtonOK.Click += new EventHandler(this.linkButtonOK_Click);
            htmlTableCell.Controls.Add(linkButtonOK);

			LinkButton linkButtonClose = new LinkButton() { ID = "LinkButtonClose_ModalPanel", Text = Resources.Ctrl_CloseButtonText };
            linkButtonClose.Click += new EventHandler(this.linkButtonClose_Click);
            htmlTableCell.Controls.Add(linkButtonClose);

            return panelPopupHeader;
        }

        private void ValidateProperties()
        {
            if (string.IsNullOrEmpty(this.HeaderText))
            {
                throw new ArgumentException("HeaderText");
            }

            if (string.IsNullOrEmpty(this.TargetControlID))
            {
                throw new ArgumentException("TargetControlID");
            }
        }

        private string GetIconUrl()
        {
            return string.Format("{0}/App_Themes/{1}/Images/container-header.png", WebUtility.WebSiteBaseUrl, this.Page.Theme);
        }

        private static Control FindControl(Control parentControl, string controlId)
        {
            if (parentControl == null) return null;

            foreach (Control childControl in parentControl.Controls)
            {
                if (childControl.ID == controlId) return childControl;
                Control control = FindControl(childControl, controlId);
                if (control != null) return control;
            }

            return null;
        }
    }
}
